package cn.edu.scau.cmi.yangzhengkai.client.classicsDesignPattern;

import java.util.Optional;

import cn.edu.scau.cmi.yangzhengkai.composite.safe.SafeComponent;
import cn.edu.scau.cmi.yangzhengkai.composite.safe.SafeComposite;
import cn.edu.scau.cmi.yangzhengkai.composite.safe.SafeLeaf;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.DialogEvent;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class SafeCompositeClient extends Application{
	
	private SafeComponent selectedComponent;
	private TreeItem<SafeComponent> selectedItem;
	
	@Override
	public void start(Stage primaryStage){
		
		AnchorPane ap = new AnchorPane();
		TreeView<SafeComponent> tv = new TreeView<SafeComponent>();
		TreeItem<SafeComponent> ti = new TreeItem<SafeComponent>(new SafeComposite("华南农业大学"));
		tv.setRoot(ti);
		tv.setPrefSize(500, 600);
	
	           
	          
		Button bu1 = new Button("添加组合件");
		bu1.setPrefSize(100, 75);
		Button bu2 = new Button("添加叶节点");
		bu2.setPrefSize(100, 75);
		Button bu3 = new Button("删除");
		bu3.setPrefSize(100, 75);
		ap.getChildren().addAll(tv,bu1,bu2,bu3);
		
		
		bu1.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				selectedItem = tv.getSelectionModel().getSelectedItem();
				
				if(selectedItem==null) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.headerTextProperty().set("Please select a component!");
					alert.showAndWait();
				}
				else {
				selectedComponent = tv.getSelectionModel().getSelectedItem().getValue();
				if(selectedComponent.getClass() == SafeLeaf.class) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.headerTextProperty().set("Can not select a SafeLeaf Object!");
					alert.showAndWait();
				}
				
				else {
				TextInputDialog dialog = new TextInputDialog();
				dialog.setTitle("Text Input Dialog");
				dialog.setHeaderText("Please enter a composite:");
				Optional<String> result = dialog.showAndWait();
				if(!result.isPresent()) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.headerTextProperty().set("Please enter a component!");
					alert.showAndWait();
				}
				else {
				SafeComposite sc = new SafeComposite(dialog.getResult(), selectedComponent);
				TreeItem<SafeComponent> t = new TreeItem<SafeComponent>(sc);
				
				SafeComposite ss = (SafeComposite)selectedComponent;
				ss.addChild(sc);
				
				selectedItem.getChildren().add(t);}}
				}
			}
		});
		
		
		bu2.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
                selectedItem = tv.getSelectionModel().getSelectedItem();
				
				if(selectedItem==null) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.headerTextProperty().set("Please select a component!");
					alert.showAndWait();
				}
				else {
				selectedComponent = tv.getSelectionModel().getSelectedItem().getValue();
				if(selectedComponent.getClass() == SafeLeaf.class) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.headerTextProperty().set("Can not select a leaf Object!");
					alert.showAndWait();
				}
				
				else {
				TextInputDialog dialog = new TextInputDialog();
				dialog.setTitle("Text Input Dialog");
				dialog.setHeaderText("Please enter a leaf:");
				Optional<String> result = dialog.showAndWait();
				
				if(!result.isPresent()) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.headerTextProperty().set("Please enter a component!");
					alert.showAndWait();
				}
				else {
				SafeLeaf sl = new SafeLeaf(dialog.getResult(), selectedComponent);
				TreeItem<SafeComponent> t = new TreeItem<SafeComponent>(sl);
				
				SafeComposite ss = (SafeComposite)selectedComponent;
				ss.addChild(sl);
				
				selectedItem.getChildren().add(t);}
				}
				}

				
				
			}
		});
		
		
		bu3.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				selectedItem = tv.getSelectionModel().getSelectedItem();
				
				if(selectedItem==null) {
					Alert alert = new Alert(AlertType.ERROR);
					alert.headerTextProperty().set("Please select a component!");
					alert.showAndWait();
				}
				
				else {
					selectedComponent = tv.getSelectionModel().getSelectedItem().getValue();
					SafeComposite parent = (SafeComposite)selectedComponent.getParent();
					selectedItem.getParent().getChildren().remove(selectedItem);
					parent.removeChild(selectedComponent);
				}
				
			}
		});
		
		
		Scene scene = new Scene(ap);
		primaryStage.setScene(scene);
		primaryStage.setWidth(1000);
		primaryStage.setHeight(600);
		primaryStage.setResizable(false);
		primaryStage.show();
		ap.setRightAnchor(bu1, 200.0);
		ap.setTopAnchor(bu1, 50.0);
		ap.setRightAnchor(bu2, 200.0);
		ap.setTopAnchor(bu2, 200.0);
		ap.setRightAnchor(bu3, 200.0);
		ap.setTopAnchor(bu3, 350.0);
	}
	
	public static void main(String[] args) {
		launch(args);
	}
	
}
